#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int N = 1510, M = 5e4 + 10;

typedef long long LL;

int n, m;

int h[N], e[M], ne[M], w[M], idx;

LL dist[N];
bool st[N];

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

LL spfa()
{
	queue<int> q;
	
	q.push(1);
	st[1] = true;
	
	memset(dist, -0x3f, sizeof dist);
	dist[1] = 0;
	
	while (q.size())
	{
		int a = q.front(); q.pop();
		
		st[a] = false;
		
		for (int i = h[a]; ~i; i = ne[i])
		{
			int j = e[i];
			
			if (dist[j] < dist[a] + w[i])
			{
				dist[j] = dist[a] + w[i];
				if (!st[j])
				{
					st[j] = true;
					q.push(j);
				}
				
			}
		}
	}
	
	return dist[n];
}

int main()
{
	cin >> n >> m;
	
	memset(h, -1, sizeof h);
	
	while (m -- )
	{
		int a, b, c;
		cin >> a >> b >> c;
		
		add(a, b, c);
	}
	
	LL t = spfa();
	
	if (t < -0x3f3f3f3f / 2) cout << -1 << endl;
	else cout << t << endl;
	
	return 0;
}